<?php
require_once('Vega/Dao/Table.php');

class Mobitv_Admin_WorldcupDao extends Vega_Dao_Table
{
    public function getDetail($matchId)
    {
        $matchId = intval($matchId);

        $sql = "SELECT * FROM wc_match WHERE id=$matchId";
        $this->_db->query($sql);

        return $this->_db->fetchRow();
    }
    
    public function getList()
    {
        $sql = "SELECT wm.*, c.name AS channel_name FROM wc_match wm LEFT JOIN channel c ON c.id=wm.channel_id ORDER BY wm.start_time";
        $this->_db->query($sql);
        
        return $this->_db->fetchRowSet();
    }

    public function getChannelList()
    {
        $sql = "SELECT id, name FROM channel";
        $this->_db->query($sql);

        return $this->_db->fetchRowset();
    }

    public function countAll()
    {
        $this->_db->query("SELECT count(*) AS `count` FROM wc_user");
        $row = $this->_db->fetchRow();

        return $row ? $row['count'] : 0;
    }

    public function insert($data)
    {
        $channelId = intval($data['channel_id']);
        $title = $this->_db->valueQuote($data['title']);
        $startTime = $this->_db->valueQuote($data['start_time']);
        $endTime = $this->_db->valueQuote($data['end_time']);

        $sql = "INSERT INTO wc_match SET title=$title, start_time=$startTime, end_time=$endTime, channel_id=$channelId, created_time=$this->now, updated_time=$this->now";
        
        return $this->_db->queryWrite($sql);
    }

    public function save($data, $matchId)
    {
        $matchId = intval($matchId);
        $channelId = intval($data['channel_id']);
        $title = $this->_db->valueQuote($data['title']);
        $startTime = $this->_db->valueQuote($data['start_time']);
        $endTime = $this->_db->valueQuote($data['end_time']);

        $sql = "UPDATE wc_match SET title=$title, start_time=$startTime, end_time=$endTime, channel_id=$channelId, updated_time=$this->now WHERE id=$matchId";
        return $this->_db->queryWrite($sql);
    }

    public function deleteFromList($list)
    {
        $list = "(" . implode(",", $list) . ")";
        $sql = "DELETE FROM wc_match WHERE id IN $list";

        return $this->_db->queryWrite($sql);
    }
}